<template>
  <view class="loading-container" :class="{ 'loading-container--fullscreen': fullscreen }">
    <view class="loading-spinner" :class="[`loading-spinner--${type}`, `loading-spinner--${size}`]">
      <view v-if="type === 'circular'" class="loading-circular"></view>
      <view v-else-if="type === 'spinner'" class="loading-dot-spinner">
        <view v-for="i in 12" :key="i" class="loading-dot-spinner__dot"></view>
      </view>
    </view>
    <view v-if="text" class="loading-text">{{ text }}</view>
  </view>
</template>

<script lang="ts">
import { defineComponent } from 'vue';

export default defineComponent({
  name: 'Loading',
  props: {
    // 加载类型：circular (环形) | spinner (菊花)
    type: {
      type: String,
      default: 'circular',
      validator: (value: string) => ['circular', 'spinner'].includes(value)
    },
    // 大小：small | medium | large
    size: {
      type: String,
      default: 'medium',
      validator: (value: string) => ['small', 'medium', 'large'].includes(value)
    },
    // 加载文案
    text: {
      type: String,
      default: ''
    },
    // 是否全屏显示
    fullscreen: {
      type: Boolean,
      default: false
    }
  }
});
</script>

<style lang="scss">
.loading-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;

  &--fullscreen {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(255, 255, 255, 0.9);
    z-index: 9999;
  }
}

.loading-spinner {
  position: relative;

  &--small {
    width: 24px;
    height: 24px;
  }

  &--medium {
    width: 36px;
    height: 36px;
  }

  &--large {
    width: 48px;
    height: 48px;
  }
}

.loading-circular {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  border: 3px solid transparent;
  border-top-color: var(--primary-color, #1890ff);
  animation: loading-rotate 1s linear infinite;
}

.loading-dot-spinner {
  position: relative;
  width: 100%;
  height: 100%;

  &__dot {
    position: absolute;
    width: 15%;
    height: 15%;
    border-radius: 50%;
    background-color: var(--primary-color, #1890ff);
    animation: loading-dot-fade 1.2s linear infinite;

    @for $i from 0 through 11 {
      &:nth-child(#{$i + 1}) {
        animation-delay: -1.2s + 0.1s * $i;
        transform: rotate($i * 30deg) translate(0, -150%);
      }
    }
  }
}

.loading-text {
  margin-top: 8px;
  font-size: 28px;
  color: #999;
}

@keyframes loading-rotate {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

@keyframes loading-dot-fade {
  0%, 39%, 100% {
    opacity: 0.2;
  }
  40%, 60% {
    opacity: 1;
  }
}
</style>
